﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConcordWinManager
{
    public class Rezident
    {
         #region [Constructors]

        public Rezident()
        {
            id_rezident = 0;
            Name = string.Empty;
            Name2 = string.Empty;
            Details = string.Empty;
            Note = string.Empty;
            this.Type = string.Empty;
            this.INN = string.Empty;
            this.KPP = string.Empty;
            this.OGRN = string.Empty;
            this.n_account = string.Empty;
            this.kor_account = string.Empty;
            this.bank_name = string.Empty;
            this.BIK = string.Empty;
            this.director = string.Empty;
            this.acting_of_the_basis = string.Empty;
            this.ur_address = string.Empty;
            this.post_address = string.Empty;
            this.tel_number = string.Empty;
            this.fax_number = string.Empty;
            this.email = string.Empty;
        }

        public Rezident(int id)
        {
            SelectById(id);
        }


        #endregion

        #region [Fields]

        /// <summary>
        /// id резидента в БД
        /// </summary>
        public int id_rezident;

        /// <summary>
        /// Название для программы
        /// </summary>
        public string Name;

        /// <summary>
        /// Название для печати на документах
        /// </summary>
        public string Name2;

        /// <summary>
        /// Реквизиты
        /// </summary>
        public string Details;


        /// <summary>
        /// Примечание
        /// </summary>
        public string Note;

        #region[Реквизиты]
        /// <summary>
        /// Тип ИП/ЮЛ
        /// </summary>
        public string Type;

        /// <summary>
        /// ИНН
        /// </summary>
        public string INN;

        /// <summary>
        /// КПП
        /// </summary>
        public string KPP;

        /// <summary>
        /// ОГРН/ОГРНИП
        /// </summary>
        public string OGRN;

        /// <summary>
        /// Номер счета
        /// </summary>
        public string n_account;

        /// <summary>
        /// Кор/счет
        /// </summary>
        public string kor_account;

        /// <summary>
        /// Банка название
        /// </summary>
        public string bank_name;

        /// <summary>
        /// БИК
        /// </summary>
        public string BIK;

        /// <summary>
        /// Директор
        /// </summary>
        public string director;

        /// <summary>
        /// Действующий на основании
        /// </summary>
        public string acting_of_the_basis;

        /// <summary>
        /// Юр.адрес
        /// </summary>
        public string ur_address;

        /// <summary>
        /// Почтовый адрес
        /// </summary>
        public string post_address;

        /// <summary>
        /// Телефон
        /// </summary>
        public string tel_number;

        /// <summary>
        /// Факс
        /// </summary>
        public string fax_number;

        /// <summary>
        /// Мыло
        /// </summary>
        public string email;
        #endregion
        // public string RezidentText;

        #endregion

        #region [Methods]


        /// <summary>
        /// Добавление в бд
        /// </summary>
        public void Insert()
        {
            string query = string.Format("INSERT INTO dbo.Rezident " +
                " (Name, Name2, Details, Note, Type, INN, KPP, OGRN, n_account, kor_account, bank_name, BIK, director,acting_of_the_basis, ur_address, post_address, tel_number, fax_number, email) " +
                " VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}')",
                this.Name,
                this.Name2,
                this.Details,
                this.Note,
                this.Type,
                this.INN,
                this.KPP,
                this.OGRN,
                this.n_account,
                this.kor_account,
                this.bank_name,
                this.BIK,
                this.director,
                this.acting_of_the_basis,
                this.ur_address,
                this.post_address,
                this.tel_number,
                this.fax_number,
                this.email
                );

            DB.execute_sql(query);
            this.id_rezident = DB.max_num("dbo.Rezident", "id_rezident");
        }

        public Rezident SelectById(int id)
        {
            
            DataSet ds = new DataSet();
            DB.query_table("SELECT * FROM dbo.Rezident WHERE id_rezident = " + id, "dbo.Rezident", ref ds);

            if (ds.Tables[0].Rows.Count != 0)
            {
                if (int.TryParse(ds.Tables["dbo.Rezident"].Rows[0]["id_rezident"].ToString(), out this.id_rezident))
                {
                    ;
                }
                this.Name = ds.Tables["dbo.Rezident"].Rows[0]["Name"].ToString();
                this.Name2 = ds.Tables["dbo.Rezident"].Rows[0]["Name2"].ToString();
                this.Details = ds.Tables["dbo.Rezident"].Rows[0]["Details"].ToString();
                this.Note = ds.Tables["dbo.Rezident"].Rows[0]["Note"].ToString();

                this.Type = ds.Tables["dbo.Rezident"].Rows[0]["Type"].ToString();
                this.INN = ds.Tables["dbo.Rezident"].Rows[0]["INN"].ToString();
                this.KPP = ds.Tables["dbo.Rezident"].Rows[0]["KPP"].ToString();
                this.OGRN = ds.Tables["dbo.Rezident"].Rows[0]["OGRN"].ToString();
                this.n_account = ds.Tables["dbo.Rezident"].Rows[0]["n_account"].ToString();
                this.kor_account = ds.Tables["dbo.Rezident"].Rows[0]["kor_account"].ToString();
                this.bank_name = ds.Tables["dbo.Rezident"].Rows[0]["bank_name"].ToString();
                this.BIK = ds.Tables["dbo.Rezident"].Rows[0]["BIK"].ToString();
                this.director = ds.Tables["dbo.Rezident"].Rows[0]["director"].ToString();
                this.acting_of_the_basis = ds.Tables["dbo.Rezident"].Rows[0]["acting_of_the_basis"].ToString();
                this.ur_address = ds.Tables["dbo.Rezident"].Rows[0]["ur_address"].ToString();
                this.post_address = ds.Tables["dbo.Rezident"].Rows[0]["post_address"].ToString();
                this.tel_number = ds.Tables["dbo.Rezident"].Rows[0]["tel_number"].ToString();
                this.fax_number = ds.Tables["dbo.Rezident"].Rows[0]["fax_number"].ToString();
                this.email = ds.Tables["dbo.Rezident"].Rows[0]["email"].ToString();

                return this;
            }
            else
            {
                return null;
            }
        }

        public void SelectAll(ref DataSet ds)
        {
            DB.query_table("SELECT * FROM dbo.Rezident ", "dbo.Rezident", ref ds);
        }

        public void SelectByArgs()
        {
            ;
        }

        public void Update()
        {
            string query = string.Format("UPDATE dbo.Rezident SET Name='{0}', " +
                " Details='{1}', Note='{2}', Name2='{3}', Type='{5}', INN='{6}', KPP='{7}',OGRN='{8}',n_account='{9}',kor_account='{10}', " +
                " bank_name='{11}',BIK='{12}',director='{13}',acting_of_the_basis='{14}',ur_address='{15}',post_address='{16}',tel_number='{17}',fax_number='{18}', email='{19}' WHERE (id_rezident= '{4}')",
                this.Name,
                this.Details,
                this.Note,
                this.Name2,
                this.id_rezident,

                this.Type,
                this.INN,
                this.KPP,
                this.OGRN,
                this.n_account,
                this.kor_account,
                this.bank_name,
                this.BIK,
                this.director,
                this.acting_of_the_basis,
                this.ur_address,
                this.post_address,
                this.tel_number,
                this.fax_number,
                this.email                
                );

            DataSet ds = new DataSet();

            DB.query_table(query, "dbo.Rezident", ref ds);

        }

        public void DeleteById()
        {
            ;
        }

        ////ВОобще хз зачем метод!
        //public string RebuildRezidentText()
        //{
        //    //return string.Format("{0} {1} {2}{3} {4}{5} {6}{7}{8}",
        //    //        this.regionName,
        //    //        this.regionName != "" ? this.regionType + "," : "",
        //    //        this.nasPunktName != "" ? this.nasPunktType + "." : "",
        //    //        this.nasPunktName != "" ? this.nasPunktName + "," : "",
        //    //        this.streetName != "" ? this.streetType + "." : "",
        //    //        this.streetName != "" ? this.streetName + "," : "",
        //    //        this.homeNumber != "" ? "д." + this.homeNumber + "," : "",
        //    //        this.buildNumber != "" ? " корп." + this.buildNumber + "," : "",
        //    //        this.flatNumber != "" ? " кв." + this.flatNumber : ""
        //    //        );

        //    return "";
        //}

    #endregion

    }
}
